/**
 * Created by lwy on 2017/6/10.
 */

var provinceId=getItem('provinceId');
var cityId=getItem('cityId');
var countyId=getItem('countyId');
var page=0;
var shopNum;

$(function(){
    if(provinceId==undefined){
        prvinceId='';
    }
    if(cityId==undefined){
        cityId='';
    }
    if(countyId==undefined){
        countyId='647';
    }
    doAjax();
})

function doAjax(){
    page++;
    $.ajax({
        url:'/shopC/getShopByCategory.json',
        dataType:'json',
        type:'post',
        data:{
            prvinceId:prvinceId,
            cityId:cityId,
            countyId:countyId,
            page:page,
        },
        success:function(data){
            if(data.code==1){
                var data = data.datas;
                for(var i=0;i<data.length;i++){
                    $('<a><li class="mui-clearfix shoplist" id="'+data[i].ID+'">'+
                        '<img src="'+data[i].MinImg+'">'+
                        '<p class="shop_name">'+data[i].ShopName+'</p>'+
                        '<p class="shop_owner">姓名：'+data[i].RealName+'</p>'+
                        '<p class="shop_phone">电话：'+data[i].Phone+'</p>'+
                        '<p class="shop_address">地址：'+data[i].ShopAddress+'</p>'+
                        '</li></a>').appendTo($('#nearbyShop'));
                }
                shopNum=data.length;
            }
        }
    })
    return shopNum;
}

mui('body').on('tap','.shoplist',function(){
    var id = $(this).attr('id');
    window.location.href="shop_detail.html?id="+id;
})

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doAjax();;
                    if (numResult < 20) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});